package com.example.controller;

import com.example.entity.specification;
import com.example.util.PageResult;
import com.example.util.R;
import com.example.vo.specificationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.service.specificationService;
import java.util.List;

@RestController
@RequestMapping("/tbspecification")
//@CrossOrigin
public class SpecificationController {

    @Autowired
    specificationService tbSpecificationService;
    //查询所有的规格列表
    @GetMapping("/list")
    public List<specification> list(){
        return tbSpecificationService.findAll();
    }
    @GetMapping("/findByPage")
    public PageResult<specification> findByPage(@RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize){
        return tbSpecificationService.findByPage(page,pageSize);
    }
    //分页查找规格，其中page,pageSize为必须参数，规格对象为可选参数
    @PostMapping("/search")
    public PageResult<specification> search(@RequestParam("page") Integer page,
                                            @RequestParam("pageSize") Integer pageSize,
                                            @RequestBody(required = false) specificationVO tbSpecificationVO){
        return tbSpecificationService.search(page,pageSize,tbSpecificationVO);
    }

    // 4. 保存规格
    @PostMapping("/save")
    public R save(@RequestBody specification tbSpecification){
        return tbSpecificationService.save(tbSpecification) ? R.ok().message("规格添加成功！") : R.error().message("规格添加失败！");
    }
}
